﻿using System.Collections.Generic;
using SharePhone.Common.Web;
using SharePhone.Core.UserProfile;
using SharePhone.Provider;
using SharePhone.Search;
using System;
//using SharePhone.UserProfileService;

namespace SharePhone.Common {
    public class ClientContext {
        private SPSearch search;

        public ClientContext() {
        }

        public ClientContext(string siteUrl) {
            Settings = new ContextSettings();
            Settings.SiteUrl = siteUrl;
            Settings.CacheTimeout = 5;
            Webs = new SPWebCollection(Settings.SiteUrl, Settings);
        }

        public ContextSettings Settings { get; set; }

        public SPWebCollection Webs { get; private set; }

        public SPSearch SearchProvider {
            get {
                if (search == null) {
                    search = new SPSearch(Settings.SiteUrl);
                    search.Credentials = Settings.Credentials;
                }
                return search;
            }
        }

        #region Internal methods

        //private static string GetCurrentUrl() {
        //    string url = HtmlPage.Document.DocumentUri.AbsoluteUri;
        //    url = url.Substring(0, url.LastIndexOf('/'));
        //    return url; 
        //}

        #endregion

        /// <summary>
        /// Loads a SPWeb object asynchronously
        /// </summary>
        /// <param name="webUrl"></param>
        /// <param name="callback"></param>
        public void GetWeb(string webUrl, SPWebLoadCompleted callback) {
            WebProvider.GetWeb(
                web => callback(this, new SPWebLoadCompletedEventArgs { Webs = new List<SPWeb> { web } }), webUrl,
                Settings);
        }

        /// <summary>
        /// Loads the root SPWeb object asynchronously
        /// </summary>
        /// <param name="callback"></param>
        public void GetRootWeb(SPWebLoadCompleted callback) {
            WebProvider.GetWeb(
                web => callback(this, new SPWebLoadCompletedEventArgs { Webs = new List<SPWeb> { web } }),
                Settings.SiteUrl, Settings);
        }

        /// <summary>
        /// Retrieves a user profile from SharePoint
        /// </summary>
        /// <param name="accountName"></param>
        /// <param name="callback"></param>
        public void GetUserProfile(string accountName, UserProfileLoaded callback) {
            UserProfileProvider.GetUserProfile(accountName, callback, Settings.SiteUrl, Settings.Credentials);
        }

        public void UpdateUserProfile(UserProfile profile, UserProfileUpdated callback) {
            UserProfileProvider.UpdateUserProfile(profile, callback, Settings.SiteUrl, Settings.Credentials);
        }
        public bool VerifyConnection() {
            try {
                GetRootWeb((s, result) =>{});
                return true;
            } catch(Exception ex) {
                return false;
            }
        }
    }
}